package com.changgou.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.service.WeixinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import com.changgou.entity.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Value("${weixin.appid}")
    private String appid;

    @Value("${weixin.partner}")
    private String partner;

    @Value("${weixin.partnerkey}")
    private String partnerkey;

    @Value("${weixin.notifyurl}")
    private String notifyurl;

    @Override
    public Map createNative(Map<String, String> parameters) {
        try {
            //1、封装参数
            Map param = new HashMap();
            param.put("appid", appid);                                  //应用ID
            param.put("mch_id", partner);                               //商户ID号
            param.put("nonce_str", WXPayUtil.generateNonceStr());       //随机数
            param.put("body", "畅购");                                   //订单描述
            param.put("out_trade_no", parameters.get("out_trade_no"));  //商户订单号
            param.put("total_fee", parameters.get("total_fee"));        //交易金额
            param.put("spbill_create_ip", "127.0.0.1");                 //终端IP
            param.put("notify_url", notifyurl);                         //回调地址
            param.put("trade_type", "NATIVE");                          //交易类型

            Map<String, String> attach = new HashMap<>();
            attach.put("username", parameters.get("username"));
            attach.put("queue", parameters.get("queue"));//队列名称
            attach.put("routingkey", parameters.get("routingkey"));//路由key
            attach.put("exchange", parameters.get("exchange"));

            //追加一个附件参数  队列名 路由key  交换机  用户名.....
            param.put("attach", JSON.toJSONString(attach));
            //2、将参数转成xml字符，并携带签名
            String paramXml = WXPayUtil.generateSignedXml(param, partnerkey);

            ///3、执行请求
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXml);
            httpClient.post();

            //4、获取参数
            String content = httpClient.getContent();
            Map<String, String> stringMap = WXPayUtil.xmlToMap(content);
            System.out.println("stringMap:" + stringMap);

            //5、获取部分页面所需参数
            Map<String, String> dataMap = new HashMap<String, String>();
            dataMap.put("code_url", stringMap.get("code_url"));
            dataMap.put("out_trade_no", parameters.get("out_trade_no"));
            dataMap.put("total_fee", parameters.get("total_fee"));

            return dataMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map queryPayStatus(String out_trade_no) {
        try {
            //1.封装参数
            Map param = new HashMap();
            param.put("appid", appid);                            //应用ID
            param.put("mch_id", partner);                         //商户号
            param.put("out_trade_no", out_trade_no);              //商户订单编号
            param.put("nonce_str", WXPayUtil.generateNonceStr()); //随机字符

            //2、将参数转成xml字符，并携带签名
            String paramXml = WXPayUtil.generateSignedXml(param, partnerkey);

            //3、发送请求
            HttpClient httpClient = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
            httpClient.setHttps(true);
            httpClient.setXmlParam(paramXml);
            httpClient.post();

            //4、获取返回值，并将返回值转成Map
            String content = httpClient.getContent();
            return WXPayUtil.xmlToMap(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map<String, String> closePay(Long orderId) throws Exception {
        //参数设置
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appid", appid); //应用ID
        paramMap.put("mch_id", partner);    //商户编号
        paramMap.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符
        paramMap.put("out_trade_no", String.valueOf(orderId));   //商家的唯一编号

        //将Map数据转成XML字符
        String xmlParam = WXPayUtil.generateSignedXml(paramMap, partnerkey);
        //确定url
        String url = "https://api.mch.weixin.qq.com/pay/closeorder";
        //发送请求
        HttpClient httpClient = new HttpClient(url);
        //https
        httpClient.setHttps(true);
        //提交参数
        httpClient.setXmlParam(xmlParam);
        //提交
        httpClient.post();
        //获取返回数据
        String content = httpClient.getContent();
        //将返回数据解析成Map
        return WXPayUtil.xmlToMap(content);
    }
}
